﻿using FastExecutor.Message.Annotation;
using FastExecutor.Message.Base.Design;
using FastExecutor.ORG.Entity;
using FastORM.Applicaiton;
using FastUtil.Base;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace FastExecutor.ORG.Log
{
    [MessageChanelAttribute(ChannelName = "Error")]
    public class ErrorLog : FastDefaultMessageHandler
    {
        public override void CustomHandle(Message.Design.Message Message)
        {
            Frame_ErrorLog LogEntity = JsonConvert.DeserializeObject<Frame_ErrorLog>(Message.MessageBody.MessageJsonContent);
            @DBUtil.Insert(LogEntity);
            DirectoryInfo SystemLogDir = new DirectoryInfo(HostingEnvironment.MapPath("~/Logs"));
            if (!SystemLogDir.Exists)
            {
                SystemLogDir.Create();
            }
            string LogFolder = Convert.ToString(Message.GetExtra("LogFolder"));
            LogUtil.WriteLog("Logs/" + LogFolder, "log_", LogEntity.ErrorContent);
            base.CustomHandle(Message);
        }

        public override bool CheckMessageIsConsume(Message.Design.Message Message)
        {
            Frame_ErrorLog LogEntity = JsonConvert.DeserializeObject<Frame_ErrorLog>(Message.MessageBody.MessageJsonContent);
            DBRow Row = new DBRow("Frame_ErrorLog", "RowGuid", LogEntity.RowGuid);
            if (Row.IsExist())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
